import { _decorator, Button, CCBoolean, easing, Enum, instantiate, math, Node, Sprite, SpriteFrame, tween, Tween, UITransform, v3, view, Widget } from "cc";
import ComponentBase from "./ComponentBase";
import { Logger } from "./Logger";
import { UI_Cache_Type, UI_Config } from "./UI_Config";
import { EventManager } from "./EventManager";
import { EngineConfig } from "./EngineConfig";

const { ccclass, property } = _decorator;

enum AniamtionType {
    Null, //不展示动画
    Pop,  //弹框模式
    //从右到左横向进入
    RightToLeft,
    //从上倒下进入
    TopToDown
}
Enum(AniamtionType);

enum StatusType {
    Yes,
    No
}
Enum(StatusType);

@ccclass("ui/ViewBase")
export default class ViewBase extends ComponentBase {

    @property(
        {
            type: StatusType,
            tooltip: "点击背景是否可以关闭 "
        }
    )
    private canBgClose: StatusType = StatusType.No; //点击背景是否可以关闭

    @property(
        {
            type: StatusType,
            tooltip: "是否需要黑色半透明背景 "
        }
    )
    private needDark: StatusType = StatusType.Yes;       //是否需要黑色半透明背景 

    @property({
        type: AniamtionType,
        tooltip: `Pop: 弹框模式
        RightToLeft: 右到左横滑进入
        TopToDown: 从上倒下进入
        `
    })
    private animationType: AniamtionType = AniamtionType.Null;

    public onlyId: number;  //实例唯一 id

    public uiConfig: UI_Config; //界面配置
    public isOpen: boolean; //是否打开着

    public contentNode: Node; //内容节点


    public onInitBase(maskLayer: Node, uiConfig: UI_Config, ...optionalParams: any[]) {
        this.uiConfig = uiConfig
        this.isOpen = true;
        let maskCloseNode: Node = this.node.getChildByName("maskCloseNode");
        Logger.log("onInitBase=", uiConfig, maskCloseNode, maskLayer)
        if (!maskCloseNode && this.needDark == StatusType.Yes) {
            maskCloseNode = instantiate(maskLayer)
            maskCloseNode.name = "maskCloseNode";
            maskCloseNode.parent = this.node
            maskCloseNode.setSiblingIndex(0)
            maskCloseNode.active = true;
            let darkWidget: Widget = maskCloseNode.getComponent(Widget);
            darkWidget.top = 0;
            darkWidget.bottom = 0
            darkWidget.left = 0;
            darkWidget.right = 0;
            maskCloseNode.getComponent(UITransform).width = screen.width
            maskCloseNode.getComponent(UITransform).height = screen.height
        }
        if (maskCloseNode) {
            if (this.canBgClose == StatusType.Yes) {
                EventManager.onAddEvent(maskCloseNode, () => {
                    //这里要抛事件给 UIManager 不然会循环引用
                    EventManager.instance.dispatch(EngineConfig.Event_Type.Event_CloseView, this)
                }, Button.Transition.NONE)
            } else {
                EventManager.onAddEvent(maskCloseNode, () => {
                }, Button.Transition.NONE)
            }
        }
        this.contentNode = this.node.getChildByName("contentNode")
        if (!this.contentNode) {
            Logger.error("该 view 不存在contentNode", this.uiConfig)
        }
        let btnClose: Node = this.contentNode.getChildByName("btnClose")
        if (btnClose) {
            EventManager.onAddEvent(btnClose, () => {
                EventManager.instance.dispatch(EngineConfig.Event_Type.Event_CloseView, this)
            })
        }

        this.onInitChild(...optionalParams);
    }

    public onInitChild(...optionalParams: any[]) {

    }

    onEnable(): void {
        super.onEnable()
        if (this.animationType == AniamtionType.Pop) {
            this.enterPop(this.contentNode);
        } else if (this.animationType == AniamtionType.RightToLeft) {
            this.enterRightToLeft(this.contentNode)
        } else if (this.animationType == AniamtionType.TopToDown) {
            this.enterTopToDown(this.contentNode)
        }
        this.onShow()
    }

    onDisable(): void {
        super.onDisable();
        this.onHide();
    }

    //弹框入场
    private enterPop(contentNode: Node) {
        if (contentNode) {
            contentNode.setScale(v3(0.8, 0.8, 0.8))
            Tween.stopAllByTarget(contentNode)
            tween(contentNode)
                .to(0.35, { scale: v3(1, 1, 1) }, { easing: easing.sineOut })
                .start();
        } else {
            Logger.error("弹窗动画时没有找到content节点")
        }
    }

    //右侧横向滑动入场
    private enterRightToLeft(contentNode: Node) {
        if (contentNode) {
            let widget: Widget = contentNode.getComponent(Widget)
            let widgetEnable: boolean = false;
            if (widget) {
                widgetEnable = widget.enabled;
                widget.enabled = false;
                contentNode.getComponent(UITransform).width = view.getVisibleSize().width;
            }

            contentNode.position = v3(view.getVisibleSize().width, 0)
            Tween.stopAllByTarget(contentNode)
            tween(contentNode)
                .to(0.35, { position: v3(0, 0, 0) }, { easing: easing.sineOut })
                .call(() => {
                    if (widget && widgetEnable) {
                        widget.enabled = widgetEnable
                    }
                })
                .start();
        } else {
            Logger.error("弹窗动画时没有找到content节点")
        }

    }

    private enterTopToDown(contentNode: Node) {
        if (contentNode) {
            let widget: Widget = contentNode.getComponent(Widget)
            let widgetEnable: boolean = false;
            if (widget) {
                widgetEnable = widget.enabled;
                widget.enabled = false;
                contentNode.getComponent(UITransform).width = view.getVisibleSize().width;
            }
            contentNode.position = v3(0, view.getVisibleSize().height)
            Tween.stopAllByTarget(contentNode)
            tween(contentNode)
                .to(0.35, { position: v3(0, 0, 0) }, { easing: easing.sineOut })
                .call(() => {
                    if (widget && widgetEnable) {
                        widget.enabled = widgetEnable
                    }
                })
                .start();

        } else {
            Logger.error("弹窗动画时没有找到content节点")
        }
    }

    //由 UIManager 调用，如果界面自己调用有些缓存没释放掉 
    public closeView() {
        if (this.isOpen) {
            this.isOpen = false;
            if (this.uiConfig.isSingle && this.uiConfig.cacheType != UI_Cache_Type.noCache) {
                this.node.active = false;
            } else {
                this.node.destroy();
            }
        }
    }

    // active为true时调用
    protected onShow() { };
    // active为false时调用
    protected onHide(callfunc?: Function) { };


}
